import { Emitter } from "egret/base/common/event";
import { IDisposable } from "egret/base/common/lifecycle";

export const enum GLOBAL_EVENT {
	on_game_cmd_upd,
}

export class Observer<T> {

	private evtCache: { [idx: number]: Emitter<T> } = {};

	public event(eType: GLOBAL_EVENT, listener: (e: T) => any, thisArgs?: any, disposables?: IDisposable[]): IDisposable {
		const emitter = this.evtCache[eType] || (this.evtCache[eType] = new Emitter<T>());
		return emitter.event(listener, thisArgs, disposables);
	}

	public fire(eType: GLOBAL_EVENT, event?: T): any {
		return this.evtCache[eType]?.fire(event);
	}

	public dispose(eType: GLOBAL_EVENT) {
		this.evtCache[eType]?.dispose();
	}
}

export const globalObserver = new Observer<any>();
